import type {
  PointInfo,
  SaveOrUpdatePointParams,
} from '@/types'

const controller = getAjaxBaseUrl('/point', '/energy')

// 点位管理
/** 查询点位树 */
export function loadTreePoint(needFullName?: 0 | 1) {
  return get<null | PointInfo[]>(`${controller}/loadTreePoint`, {
    needFullName,
  })
}

/** 根据ID查询点位信息 */
export function loadPoint(id: number) {
  return get(`${controller}/loadPoint?id=${id}`)
}

/** 根据ID删除点位信息 */
export function deletePoint(id: number) {
  return get(`${controller}/deletePoint?id=${id}`)
}

/** 新增或修改点位信息 */
export function saveOrUpdatePoint(params: SaveOrUpdatePointParams) {
  return post(`${controller}/saveOrUpdatePoint`, params)
}

/** 排序用能单元 */
export function sortEnergyUnit(id: number, targetId: number) {
  return post(`${controller}/sequence/${id}/${targetId}`)
}

/** 移动用能单元 */
export function moveEnergyUnit(id: number, targetId: number) {
  return post(`${controller}/move/${id}/${targetId}`)
}

/** 拖拽用能单元 */
export function dragEnergyUnit(id: number, targetId: number, type: 'before' | 'after' | 'inner') {
  return post(`${controller}/drag/${type}/${id}/${targetId}`)
}

/** 角色配置用能单元-查询所有用能单元树 */
export function loadTreeEnergyUnit() {
  return get(`${controller}/loadAllTreePoint`)
}

/** 角色配置用能单元-查询角色配置的用能单元树 */
export function loadTreeEnergyUnitByRoleId(roleId: number) {
  return get(`${controller}/loadPointIdsListByRole?roleId=${roleId}`)
}

/** 角色配置用能单元-保存角色配置的用能单元树 */
export function saveRoleEnergyUnit(roleId: number, pointIds: string[]) {
  return post(`${controller}/batchByRolePoint`, {
    roleId,
    pointIds,
  })
}

/** 角色配置用能单元-查看当前角色配置的用能单元树 */
export function loadRoleEnergyUnit(needFullName = 1) {
  return get(`${controller}/loadTreePointLast?needFullName=${needFullName}`)
}
